'use strict';
	angular.module('inventory.invCBKZSingle', [])

	.controller('invCBKZSingleCtrl'
			,['$rootScope', '$scope','systemService'
			  , 'BizData', 'apiService'
			   , f])//控制器结束
	function f($rootScope,$scope, systemService
					   , BizData, apiService
					   ) {
		var common = require('common');
		
		var Model = this;
//		$scope.path = $rootScope.path;
//		$scope.createPermission = $rootScope.path + ':create';
		
		//定义页面级别的变量
		var systemCode = $rootScope.winid; //系统代码

		var flowType = 0;
		var billType;
		var direction;
		var dealType = 0;
		var billTypeName = "";
		var originName = "";

		var autoCode; //是否自动编码
		var revOpay; //入库出库
		var costingCode; //影响成本码
		var storageTypeD ;
		var storageTypeDLabel ;
		var storageType ;
		var storageTypeLabel ;


		/////////////////////////////////////////////////////////////////////////////////////////////////

//		var departmentNo;
//		var departmentName;
		var employeeNo;
		var employeeName;
		//var targetNo;
		//var targetName;

		var recordNo;
		var maxDetailNo;
		var serialNo;
		
		var storageID;
		var storageNo;
		var storageName;
		var floorNo;

		var origin;

		var factoryID;
		var factoryNo;
		var factoryName;
		
		var billNo;

		var state;
		var status;
		var audit;
		var orderDate;
		
		var userId;
		var enterpriseNo;
		var enterpriseName;
		
		var providerID;
		var providerNo;
		var shortName
		var providerName;
		
		
		
		
		Model.billTypeListClicked = function(event){
			console.log('Model.listClicked-->>',event);
			var value = event.item.value;
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
		
			var billType = value;
			var enterpriseNo = dataMaster.getValue("enterpriseNo", rowID);
			//获取单据参数
			var options = {};
			options.billType = billType;
			options.ognID = enterpriseNo;
			options.async = false;//同步调用
			var billParam = systemService.getBillParam(options);
			if (billParam.state == "success"){
				dataMaster.setValue('dealType',billParam.dealType,rowID);
				dataMaster.setValue('billTypeName',billParam.billTypeName,rowID);
				dataMaster.setValue('flowType',billParam.flowType,rowID);
				dataMaster.setValue('origin', billParam.origin, rowID);	
				dataMaster.setValue('originName', billParam.originName, rowID);	
				dataMaster.setValue('revOpay', billParam.revOpay, rowID);	
				dataMaster.setValue('direction', billParam.direction, rowID);
				dataMaster.setValue('costingCode', billParam.costingCode, rowID);	
				
				if (billParam.autoCode = "Y"){
					//产生自动流水号
					dataMaster.setValue('recordNo',billParam.billNo,rowID);
				}
			}else{
				systemService.dialogMsg('请在单据设置中进行初始化。');
			}
		};

		
		/**
		name:bizData#onNewCreateParam
		description: <b>[回调型事件]</b>业务新增数据创建新增参数事件，可以增加和修改用户自定义的参数
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"param" : {justep.Request.ActionParam} 新增参数对象,
			"defaultValues" : array - 新增行的默认值数组 ，数组的长度决定新增行数，数组中的对象是列的默认值
				defaultValues格式：
					[
						{列名:默认值,...},
						{列名:默认值,...},
						...
					]
		}
		</xmp>	
		@see justep.XData#newData
		@see justep.Request.ActionParam
		*/
		Model.dataMasterNewCreateParam = function(event) {
			//event.param = {"billType": billType};
		};
		
		/**
		name:bizData#onBeforeNew
		description: <b>[回调型事件]</b>业务数据新增前
		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
		 */
		Model.dataMasterBeforeNew = function(event) {

		};
		/**
		 * name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
		 * 
		 * @param event
		 *            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
		 */
		Model.dataMasterAfterNew = function(event){
			//创建行后赋值
			event.source.setValue('systemCode',systemCode,event.id);
			
			event.source.setValue('storageTypeD', 'pro', event.id);	
			event.source.setValue('storageTypeDLabel', '供应商', event.id);	
			event.source.setValue('storageType', 'chc', event.id);	
			event.source.setValue('storageTypeLabel', '存货仓', event.id);	
			
		};

		/**
		name:bizData#onBeforeDelete
		description: <b>[回调型事件]</b>业务数据删除前
		@param {object} event 它的结构如下:<br/>
		{
			"source":组件的js对象
			,"cancel":可修改	，设置为true后中断当前delete动作
			,"deleteIDList":删除的行Id数组
		}
		*/
		Model.dataMasterBeforeDelete = function(event) {
//			alert(event.deleteIDList[0]);
		};
		/**
		name:bizData#onAfterDelete
		description: <b>[回调型事件]</b>业务数据删除后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"deleteIDList" : 删除的行Id数组
		}
		</xmp>	
		*/
		Model.dataMasterAfterDelete = function(event) {
//			alert('删除之后！')
		};

		
		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"rowID" : 行Id,
			"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataMasterIndexChanged = function(event) {
			if(event.rowID!=null){
				Model.dataDetail.fMasterID = event.rowID;
	       		Model.dataDetail.refreshData();
			}else{
				Model.dataDetail.clearData();
			}
		};
		
		/**
		name:bizData#onDataChanged
		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
			"selfChanged" : 是否自身变化，false：从data触发的变化,
			"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
		}
		</xmp>	
		*/
		Model.dataMasterDataChanged = function(event) {
			
		};
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		//输入值变化，监视input的输入，ng-change
		Model.dataMasterValueChanged = function(event) {

			if(event.column=='orderDate'){
				var rowID = event.source.getID(event.rowIndex);
				var orderDate = event.source.getValue('orderDate',rowID);
				var dataDetail = Model.dataDetail;
				for(var i=0;i<dataDetail.getCount();i++){
					dataDetail.setValue('orderDate',orderDate,dataDetail.getID(i));
				}
			}
			if(event.column=='factoryNo'){
				var rowID = event.source.getID(event.rowIndex);
				var factoryID = event.source.getValue('factoryID',rowID);
				var factoryNo = event.source.getValue('factoryNo',rowID);
				var factoryName = event.source.getValue('factoryName',rowID);
				var dataDetail = Model.dataDetail;
				for(var i=0;i<dataDetail.getCount();i++){
					dataDetail.setValue('factoryID',factoryID,dataDetail.getID(i));
					dataDetail.setValue('factoryNo',factoryNo,dataDetail.getID(i));
					dataDetail.setValue('factoryName',factoryName,dataDetail.getID(i));
				}
			}
			if(event.column=='employeeNo'){
				var rowID = event.source.getID(event.rowIndex);
				var employeeNo = event.source.getValue('employeeNo',rowID);
				var employeeName = event.source.getValue('employeeName',rowID);
				var dataDetail = Model.dataDetail;
				for(var i=0;i<dataDetail.getCount();i++){
					dataDetail.setValue('employeeNo',employeeNo,dataDetail.getID(i));
					dataDetail.setValue('employeeName',employeeName,dataDetail.getID(i));
				}
			}
			if(event.column=='providerNo'){
				var rowID = event.source.getID(event.rowIndex);
				var storageTypeD = event.source.getValue('storageTypeD',rowID);
				var storageTypeDLabel = event.source.getValue('storageTypeDLabel',rowID);
				var providerID = event.source.getValue('providerID',rowID);
				var providerNo = event.source.getValue('providerNo',rowID);
				var shortName = event.source.getValue('shortName',rowID);
				var providerName = event.source.getValue('providerName',rowID);
				var dataDetail = Model.dataDetail;
				for(var i=0;i<dataDetail.getCount();i++){
					dataDetail.setValue('storageTypeD',storageTypeD,dataDetail.getID(i));
					dataDetail.setValue('storageTypeDLabel',storageTypeDLabel,dataDetail.getID(i));
					dataDetail.setValue('providerID',providerID,dataDetail.getID(i));
					dataDetail.setValue('providerNo',providerNo,dataDetail.getID(i));
					dataDetail.setValue('shortName',shortName,dataDetail.getID(i));
					dataDetail.setValue('providerName',providerName,dataDetail.getID(i));
				}
			}


			//更新子表的全部工单号
			if(event.column=='recordNo'){
				var rowID = event.source.getID(event.rowIndex);
				var recordNo = event.source.getValue('recordNo',rowID);
				var dataDetail = Model.dataDetail;
				for(var i=0;i<dataDetail.getCount();i++){
					dataDetail.setValue('recordNo',recordNo,dataDetail.getID(i));
				}
			}
			
			if (event.column == "billType"){
				var dataMaster = event.source;
				var rowID = dataMaster.getID(event.rowIndex);
				var billType = dataMaster.getValue("billType", rowID);
				var billTypeName = dataMaster.getValue("billTypeName", rowID);

				var dataDetail = Model.dataDetail;
				for(var i=0;i<dataDetail.getCount();i++){
					dataDetail.setValue('billType',billType,dataDetail.getID(i));
					dataDetail.setValue('billTypeName',billTypeName,dataDetail.getID(i));
				}

			}
			
		};
		
		
		Model.dataMasterBeforeSave = function(event) {
			//console.log('dataMasterBeforeSave-->',event);
			//循环检查数据合法性
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
			var v = dataMaster.getValue('billType',rowID);
			//console.log('billType-->>',v);
			if(!v){
				event.cancel = true;
				alert('单据类型不能为空！');
			}
		};
		
		
		Model.dataMasterAfterSave = function(event) {
			//保存子表
//			Model.dataDetail.saveData();
		};
		
		

		/**
		name:bizData#onBeforeDelete
		description: <b>[回调型事件]</b>业务数据删除前
		@param {object} event 它的结构如下:<br/>
		{
			"source":组件的js对象
			,"cancel":可修改	，设置为true后中断当前delete动作
			,"deleteIDList":删除的行Id数组
		}
		*/
		Model.dataDetailBeforeDelete = function(event) {
//			alert(event.deleteIDList[0]);
//			event.cancel = true;
		};
		
		/**
		name:bizData#onAfterDelete
		description: <b>[回调型事件]</b>业务数据删除后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"deleteIDList" : 删除的行Id数组
		}
		</xmp>	
		*/
		Model.dataDetailAfterDelete = function(event) {
			//uServiceCenter.rkckDetailAfterDelete(event);
			
			if(event.column=="itemQuantity" 
				|| event.column=="pieceQuantity"
					|| event.column=="costingAmount"
					){
				var sum = event.source.getSum(event.column);
				var dataMaster = Model.dataMaster;
				dataMaster.setValue(event.column,sum,dataMaster.getCurrentID());
			}

		};
		
		
		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"rowID" : 行Id,
			"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataDetailIndexChanged = function(event) {

		};
		
		/**
		name:bizData#onDataChanged
		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
			"selfChanged" : 是否自身变化，false：从data触发的变化,
			"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
		}
		</xmp>	
		*/
		Model.dataDetailDataChanged = function(event) {
			
		};
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		//输入值变化，监视input的输入，ng-change
		Model.dataDetailValueChanged = function(event) {
			//console.log('Model.dataDetailValueChanged.column-->>',event.column);
			// 单位成本数据改变，重新计算合计单位成本
			if (event.column == "materialAmountPer" || event.column == "workingAmountPer"
					|| event.column == "manufactAmountPer" || event.column == "processAmountPer"
					|| event.column == "itemQuantity"
					) {
				// alert("第"+event.rowIndex+"行arriveAmount的值已改变,原值为:"+event.originalValue+",新值为:"+event.value+",rowid为:"+event.source.getID(event.rowIndex));
				var dataDetail = event.source;
				var rowID = dataDetail.getID(event.rowIndex);
				
				var itemQuantity = dataDetail.getValue("itemQuantity", rowID);
				//var direction = dataDetail.getValue("direction", rowID);
				//itemQuantity = common.accMul(itemQuantity, direction);
				
				var materialAmountPer = dataDetail.getValue("materialAmountPer", rowID);// 到货数量
				var workingAmountPer = dataDetail.getValue("workingAmountPer", rowID);// 到货数量
				var manufactAmountPer = dataDetail.getValue("manufactAmountPer", rowID);// 到货数量
				var processAmountPer = dataDetail.getValue("processAmountPer", rowID);// 到货数量
				//var costingAmountPer = common.accAdd(common.accAdd(common.accAdd(materialAmountPer,workingAmountPer),manufactAmountPer),processAmountPer);
				
				if (isNaN(materialAmountPer)) 
					materialAmountPer = 0;
				if (isNaN(workingAmountPer)) 
					workingAmountPer = 0;
				if (isNaN(manufactAmountPer)) 
					manufactAmountPer = 0;
				if (isNaN(processAmountPer)) 
					processAmountPer = 0;
				if (isNaN(costingAmountPer)){costingAmountPer = 0;} 
				
				var costingAmountPer = common.accAdd(common.accAdd(common.accAdd(materialAmountPer, workingAmountPer),manufactAmountPer),processAmountPer);
				dataDetail.setValue("costingAmountPer", costingAmountPer, rowID);
				

				//计算材料成本
				var materialAmount = common.accMul(itemQuantity, materialAmountPer);
				//计算人工成本
				var workingAmount = common.accMul(itemQuantity, workingAmountPer);
				//计算制造费用
				var manufactAmount = common.accMul(itemQuantity, manufactAmountPer);
				//计算加工费用
				var processAmount = common.accMul(itemQuantity, processAmountPer);
				//计算成本合计
				//var costingAmount = common.accMul(itemQuantity, costingAmountPer);
				var costingAmount = common.accAdd(common.accAdd(common.accAdd(materialAmount,workingAmount),manufactAmount),processAmount);
				
				dataDetail.setValue("materialAmount", materialAmount, rowID);
				dataDetail.setValue("workingAmount", workingAmount, rowID);
				dataDetail.setValue("manufactAmount", manufactAmount, rowID);
				dataDetail.setValue("processAmount", processAmount, rowID);
				dataDetail.setValue("costingAmount", costingAmount, rowID);
			}
			
			/*
			// 单位成本数据改变，重新计算成本合计
			if (event.column == "costingAmountPer" || event.column == "priceAmount"	) {
				// alert("第"+event.rowIndex+"行arriveAmount的值已改变,原值为:"+event.originalValue+",新值为:"+event.value+",rowid为:"+event.source.getID(event.rowIndex));
				var dataDetail = event.source;
				var rowID = dataDetail.getID(event.rowIndex);
				
				var priceAmount = dataDetail.getValue("priceAmount", rowID);
				var direction = dataDetail.getValue("direction", rowID);
				priceAmount = common.accMul(priceAmount, direction);
				
				var materialAmountPer = dataDetail.getValue("materialAmountPer", rowID);// 到货数量
				var workingAmountPer = dataDetail.getValue("workingAmountPer", rowID);// 到货数量
				var manufactAmountPer = dataDetail.getValue("manufactAmountPer", rowID);// 到货数量
				var processAmountPer = dataDetail.getValue("processAmountPer", rowID);// 到货数量
				var costingAmountPer = dataDetail.getValue("costingAmountPer", rowID);// 到货数量

				if (isNaN(costingAmountPer)){costingAmountPer = 0;} 
					

				//计算材料成本
				var materialAmount = common.accMul(priceAmount, materialAmountPer);
				//计算人工成本
				var workingAmount = common.accMul(priceAmount, workingAmountPer);
				//计算制造费用
				var manufactAmount = common.accMul(priceAmount, manufactAmountPer);
				//计算加工费用
				var processAmount = common.accMul(priceAmount, processAmountPer);
				//计算成本合计
				var costingAmount = common.accMul(priceAmount, costingAmountPer);
				
				dataDetail.setValue("materialAmount", materialAmount, rowID);
				dataDetail.setValue("workingAmount", workingAmount, rowID);
				dataDetail.setValue("manufactAmount", manufactAmount, rowID);
				dataDetail.setValue("processAmount", processAmount, rowID);
				dataDetail.setValue("costingAmount", costingAmount, rowID);
			}
			*/
			
			if(event.column=="itemQuantity" 
				|| event.column=="pieceQuantity"
					|| event.column=="costingAmount"
					){
				var sum = event.source.getSum(event.column);
				var dataMaster = Model.dataMaster;
				dataMaster.setValue(event.column,sum,dataMaster.getCurrentID());
			}
			
			//计算有效期至
			if(event.column=='produceDate'){
				var dataDetail = event.source;
				var rowID = dataDetail.getID(event.rowIndex);
				var produceDate = dataDetail.getValue('produceDate',rowID);
				if(produceDate!=null && produceDate!=''){
//					var d = justep.Date.fromString(produceDate,justep.Date.STANDART_FORMAT_SHOT);//'yyyy-MM-dd'//justep.Date.STANDART_FORMAT_SHOT
					var d = produceDate;//'yyyy-MM-dd'
					//alert('生产日期-->'+produceDate+'-->'+d.toString());
					//获取有效天数
					var validDays = parseInt(dataDetail.getValue('validDays',rowID));
			//		alert('有效天数-->'+validDays);
//					var validDate = justep.Date.increase(d,validDays,'d');
					var validDate = common.getNewDay(d,validDays);
					dataDetail.setValue('validDate',validDate,rowID);
					//获取复检天数
					/*
					var checkoutAgainDays = parseInt(dataDetail.getValue('checkoutAgainDays',rowID));
//					var checkoutAgainDate = justep.Date.decrease(validDate,checkoutAgainDays,'d');
					var checkoutAgainDate = common.getNewDay(validDate,-checkoutAgainDays);
//					dataDetail.setValue('checkoutAgainDate',justep.Date.toString(checkoutAgainDate,justep.Date.STANDART_FORMAT_SHOT),rowID);
					dataDetail.setValue('checkoutAgainDate',checkoutAgainDate,rowID);
					*/
				}
			}
			if(event.column=='validDate'){
				var dataDetail = event.source;
				var rowID = dataDetail.getID(event.rowIndex);
				var validDate = dataDetail.getValue('validDate',rowID);
				if(validDate!=null && validDate!=''){
					var d = validDate;//'yyyy-MM-dd'
					//获取复检天数
					var checkoutAgainDays = parseInt(dataDetail.getValue('checkoutAgainDays',rowID));
					var checkoutAgainDate = common.getNewDay(validDate,-checkoutAgainDays);
					dataDetail.setValue('checkoutAgainDate',checkoutAgainDate,rowID);
				}
			}
		};
		

		/**
		name:bizData#onBeforeNew
		description: <b>[回调型事件]</b>业务数据新增前
		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
		 */
		Model.dataDetailBeforeNew = function(event) {
			// 明细数据新增之前
			// 检查主表中的数据是否完整
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
			
			state = dataMaster.getValue('state', rowID);
			status = dataMaster.getValue('status', rowID);
			audit = dataMaster.getValue('audit', rowID);
			orderDate = dataMaster.getValue('orderDate', rowID);
			
			//////////////////////////////////////////////////////////////////////
			systemCode = dataMaster.getValue('systemCode', rowID);
			costingCode = dataMaster.getValue('costingCode', rowID);
//			dealObject = dataMaster.getValue('dealObject', rowID);
//			storeObject = dataMaster.getValue('storeObject', rowID);

			storageType = dataMaster.getValue('storageType', rowID);
			storageTypeLabel = dataMaster.getValue('storageTypeLabel', rowID);
			storageID = dataMaster.getValue('storageID', rowID);
			storageNo = dataMaster.getValue('storageNo', rowID);
			storageName = dataMaster.getValue('storageName', rowID);
			floorNo = dataMaster.getValue('floorNo', rowID);
			//////////////////////////////////////////////////////////////////////

			// 判断如果那些为空，则拒绝新增明细数据
			factoryID = dataMaster.getValue('factoryID', rowID);
			factoryNo = dataMaster.getValue('factoryNo', rowID);
			factoryName = dataMaster.getValue('factoryName', rowID);
			if (factoryNo == null || factoryNo == "") {
				// 取消新增操作
//				alert('工厂不能为空！请先选择主表中的工厂，然后再在明细表中新增行。');
				event.cancel = true;
//				systemService.dialogMsg('工厂不能为空！请先选择主表中的工厂，然后再在明细表中新增行。');
     			$rootScope.addAlert('danger','工厂不能为空！请先选择主表中的工厂，然后再在明细表中新增行。');
				
			}	
			storageTypeD = dataMaster.getValue('storageTypeD', rowID);
			storageTypeDLabel = dataMaster.getValue('storageTypeDLabel', rowID);
			// 判断如果那些为空，则拒绝新增明细数据
			providerID = dataMaster.getValue('providerID', rowID);
			providerNo = dataMaster.getValue('providerNo', rowID);
			shortName = dataMaster.getValue('shortName', rowID);
			providerName = dataMaster.getValue('providerName', rowID);
			
			/*
			if (providerNo == null || providerNo == "") {
				// 取消新增操作
//				alert('出入库部门不能为空！请先选择主表中的出入库部门，然后再在明细表中新增行。')
				event.cancel = true;
				systemService.dialogMsg('出入库部门不能为空！请先选择主表中的出入库部门，然后再在明细表中新增行。');
			}
			*/

			billType = dataMaster.getValue('billType', rowID);
			direction = dataMaster.getValue('direction', rowID);
//			departmentNo = dataMaster.getValue('departmentNo', rowID);
//			departmentName = dataMaster.getValue('departmentName', rowID);
			employeeNo = dataMaster.getValue('employeeNo', rowID);
			employeeName = dataMaster.getValue('employeeName', rowID);

			recordNo = dataMaster.getValue('recordNo', rowID);
			
			//处理从表流水号
			maxDetailNo=dataMaster.getValue('maxDetailNo', rowID);
			maxDetailNo++;
			var detailNo=common.preZeroFill(maxDetailNo, 5);
			serialNo=detailNo;
				
			dealType = dataMaster.getValue('dealType', rowID);
			billTypeName = dataMaster.getValue('billTypeName', rowID);
			
			//////////////////////////////////////////////////////////////////////
			revOpay = dataMaster.getValue('revOpay', rowID);
			//////////////////////////////////////////////////////////////////////
			
			userId = dataMaster.getValue('userId', rowID);
			enterpriseNo = dataMaster.getValue('enterpriseNo', rowID);
			enterpriseName = dataMaster.getValue('enterpriseName', rowID);

		};
		
		/**
		 * name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
		 * 
		 * @param event
		 *            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
		 */
		Model.dataDetailAfterNew = function(event){
			var dataDetail = event.source;
			var rowID = event.id;

			dataDetail.setValue('state', state, rowID);
			dataDetail.setValue('status', status, rowID);
			dataDetail.setValue('audit', audit, rowID);
			dataDetail.setValue('orderDate', orderDate, rowID);

			dataDetail.setValue('billType', billType, rowID);
			dataDetail.setValue('direction', direction, rowID);
//			dataDetail.setValue('departmentNo', departmentNo, rowID);
//			dataDetail.setValue('departmentName', departmentName, rowID);
			dataDetail.setValue('employeeNo', employeeNo, rowID);
			dataDetail.setValue('employeeName', employeeName, rowID);
//			dataDetail.setValue('targetNo', targetNo, rowID);
//			dataDetail.setValue('targetName', targetName, rowID);	

			dataDetail.setValue('storageTypeD', storageTypeD, rowID);
			dataDetail.setValue('storageTypeDLabel', storageTypeDLabel, rowID);
			dataDetail.setValue('providerID', providerID, rowID);
			dataDetail.setValue('providerNo', providerNo, rowID);
			dataDetail.setValue('shortName', shortName, rowID);
			dataDetail.setValue('providerName', providerName, rowID);
			
			dataDetail.setValue('factoryID', factoryID, rowID);	
			dataDetail.setValue('factoryNo', factoryNo, rowID);	
			dataDetail.setValue('factoryName', factoryName, rowID);	

			dataDetail.setValue('recordNo', recordNo, rowID);	

			//dataDetail.setValue('serialNo', event.id, rowID);
			dataDetail.setValue('serialNo', serialNo, rowID);

			////////////////////////////////////////////////////////////////////////////////////
			dataDetail.setValue('systemCode', systemCode, rowID);
			dataDetail.setValue('costingCode', costingCode, rowID);
//			dataDetail.setValue('dealObject', dealObject, rowID);
//			dataDetail.setValue('storeObject', storeObject, rowID);
			
			dataDetail.setValue('storageType', storageType, rowID);
			dataDetail.setValue('storageTypeLabel', storageTypeLabel, rowID);
			dataDetail.setValue('storageID', storageID, rowID);
			dataDetail.setValue('storageNo', storageNo, rowID);
			dataDetail.setValue('storageName', storageName, rowID);
			dataDetail.setValue('floorNo', floorNo, rowID);
			/////////////////////////////////////////////////////////////////////////////////////////
			
			dataDetail.setValue('dealType', dealType, rowID);
			dataDetail.setValue('billTypeName', billTypeName, rowID);
			
			////////////////////////////////////////////////////////////////////////////////////
			dataDetail.setValue('revOpay', revOpay, rowID);
			/////////////////////////////////////////////////////////////////////////////////////////
			
			//更新主表计数器（从表最大流水号）
			var dataMaster = Model.dataMaster;
			var masterRowID = dataMaster.getCurrentID();
			dataMaster.setValue('maxDetailNo', maxDetailNo, masterRowID);	
			
			//更新主表中子表记录数
			dataMaster.setValue('detailsCount', dataMaster.getValue('detailsCount', masterRowID) + 1, masterRowID);	

			dataDetail.setValue('userId', userId, rowID);
			dataDetail.setValue('enterpriseNo', enterpriseNo, rowID);
			dataDetail.setValue('enterpriseName', enterpriseName, rowID);

			dataDetail.setValue('fMasterID',dataDetail.fMasterID,rowID);

		};
		
		///////////////////按钮组开始///////////////////////////////////////////////////
		//新建
		Model.add = function(){
			
		     var options = {
			         index : 0,
			         defaultValues : [
			             {
			            	 state:'active'
			            	 ,status:'open'
			            	 ,audit:'active'
			            	 
			            	 ,itemQuantity:0
			            	 ,pieceQuantity:0
			            	 ,costingAmount:0
			            	 
			            	 ,entryFlag:0
			            	 ,retailFlag:0
			            	 ,exchangeRate:0
			            	 ,printTimes:0

			            	 ,resultStatus:'待审核'
			            	 
			            	 ,detailsCount:0
			            	 ,detailsCountState:0
			            	 ,maxDetailNo:0
			            	 
			            	 ,orderDate: ':getCurrentDate()'
				             ,employeeNo: ':getCurrentPsmFID()'
					         ,employeeName: ':getCurrentPsmFName()'
				             ,cUsercode: ':getCurrentPersonCode()'
					         ,cUsername: ':getCurrentPersonName()'
						     ,cDatetime: ':getCurrentDateTime()'
							 ,userId: ':getCurrentPersonID()'
							 ,enterpriseNo: ':getCurrentOgnFID()'
							 ,enterpriseName: ':getCurrentOgnLongName()'
							 ,version: 0

			            }//,
//			             {itemNo:'a2',itemDescribe:'b2'}
			         ]
			     };    

			Model.dataMaster.newData(options);

			if($scope.tabpanel!='single'){
				$scope.tabpanel='single'
			}
		};
		
		//修改
		Model.edit = function(){
			Model.dataMaster.editData();
			if($scope.tabpanel!='single'){
				$scope.tabpanel='single'
			}
		};
		
		//保存
		Model.save = function() {
			Model.dataMaster.saveData();
		};

		//取消编辑
		Model.saveCancel = function() {
			Model.dataMaster.editCancel();
		};
		
		/*
		//不提示直接提交后台删除当前行
		var data = justp.xbl('mainData');
		var deleteConfirm = data.deleteConfirm;
		var directDeleteMode = data.directDeleteMode;
		try{
		    data.deleteConfirm = false;
		    data.directDeleteMode = true;
		    data.deleteData(data.getID());
		}finally{
		    data.deleteConfirm = deleteConfirm;
		    data.directDeleteMode = directDeleteMode;
		}
		*/
		Model.remove = function() {
			Model.dataMaster.deleteData(Model.dataMaster.getCurrentID());
			Model.dataMaster.refreshData();
		};
		
		//刷新
		Model.refresh = function() {
			Model.dataMaster.refreshData();
		};
		
		
		//输出：在浏览器窗口打开/下载打印（打印和导出）
		Model.printExport = function() {
			var options = {};
			options.path = $rootScope.getServiceName() + $rootScope.path + "/printPDF";
			options.id = Model.dataMaster.getCurrentID();
			if($rootScope.separation==0){
				//整体项目
				systemService.openPrintExportWindow(options); 
			}else{
				//前后端分离
				systemService.downloadExport(options); 
			}
		};
		
		///////////////////按钮组结束///////////////////////////////////////////////////
		///////////////////子表按钮组开始///////////////////////////////////////////////////
		//增加
		Model.addrow = function(){
		     var options = {
			         index : Model.dataDetail.getCount(),
			         defaultValues : [
			             {
		            		validDays: 0//有效天数
		            		,checkoutAgainDays: 0//复检天数
		            		,itemQuantity: 0
		            		,pieceQuantity: 0
		            		,accountQuantityI: 0
		            		,accountQuantityO: 0
		            		,pieceQuantityI: 0
		            		,pieceQuantityO: 0
		            		,itemQuantityL: 0
		            		,costingAmountL: 0
		            		,pieceQuantityL: 0
		            		,costingAmountPerL: 0
		            		,materialAmountPer: 0
		            		,workingAmountPer: 0
		            		,manufactAmountPer: 0
		            		,processAmountPer: 0
		            		,costingAmountPer: 0
		            		,materialAmount: 0
		            		,workingAmount: 0
		            		,manufactAmount: 0
		            		,processAmount: 0
		            		,costingAmount: 0
		            		
		            		,frozenFlag: 0
					            	
			            	,batchNo:'********************'
			            }
//			            ,{itemNo:'a2',itemDescribe:'b2'}
			         ]
			     };    

			Model.dataDetail.newData(options);
		}
		Model.delrow = function() {
			 if(Model.dataDetail.directDeleteMode){
				//直接删除
				 if(Model.dataDetail.deleteData(Model.dataDetail.getCurrentID())==0){
//					 Model.dataDetail.refreshData();
				 }
			 }else{
				//前台移除
				 if(Model.dataDetail.removeData(Model.dataDetail.getCurrentID())==0){
//					 Model.dataDetail.recordState = 'delete';
				 }
			 }
		};
		///////////////////子表按钮组结束///////////////////////////////////////////////////

		//打开对话框（选择工厂）
		Model.triggerFactoryNoClick = function(){
			
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();
     		var factoryID = dataMaster.getValue('factoryID', rowID);
     		if(factoryID!=null && factoryID!=''){
         		dataMaster.setValue('factoryID', null, rowID);
         		dataMaster.setValue('factoryNo', null, rowID);
         		dataMaster.setValue('factoryName', null, rowID);
     			return;
     		}
			
			var options = {};
			options.recordState = 'none';
			options.id = dataMaster.getValue('enterpriseNo',rowID);

			var modalInstance = systemService.openFactoryNoDialog(options);
	        
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
	             try {
	             	for ( var i = 0; i < returnData.length; i++) {
	             		//alert(returnData[i].rowid+'-->'+returnData[i].billTypeName);

	             		dataMaster.setValue('factoryID', returnData[i].fID, rowID);
	             		dataMaster.setValue('factoryNo', returnData[i].factoryNo, rowID);
	             		dataMaster.setValue('factoryName', returnData[i].factoryName, rowID);
	         			
	                 }
	             } finally {
	             	return;
	             }

	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
		};
		
		//打开对话框（选择仓库）
		Model.triggerStorageNoClick = function(){
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();
     		var storageID = dataMaster.getValue('storageID', rowID);
     		if(storageID!=null && storageID!=''){
         		dataMaster.setValue('storageType', null, rowID);
         		dataMaster.setValue('storageTypeLabel', null, rowID);
         		dataMaster.setValue('storageID', null, rowID);
         		dataMaster.setValue('storageNo', null, rowID);
         		dataMaster.setValue('storageName', null, rowID);
         		dataMaster.setValue('floorNo', null, rowID);
     			return;
     		}
     		
			var options = {};
			options.id = dataMaster.getValue('enterpriseNo',rowID);
			options.factoryID = dataMaster.getValue("factoryID",rowID);
			
			var modalInstance = systemService.openStorageListDialog(options);

	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
         		dataMaster.setValue('factoryID', returnData[0].factoryID, rowID);
         		dataMaster.setValue('factoryNo', returnData[0].factoryNo, rowID);
         		dataMaster.setValue('factoryName', returnData[0].factoryName, rowID);

	     		dataMaster.setValue('storageType', returnData[0].storageType, rowID);
	     		dataMaster.setValue('storageTypeLabel', returnData[0].storageTypeLabel, rowID);
	     		dataMaster.setValue('storageID', returnData[0].fID, rowID);
	     		dataMaster.setValue('storageNo', returnData[0].storageNo, rowID);
	     		dataMaster.setValue('storageName', returnData[0].storageName, rowID);
	     		dataMaster.setValue('floorNo', returnData[0].storageFullName, rowID);

	        }, function (reason) {  
	            console.info('用户取消了操作 at: ' + new Date());  
	        });  
			
		};
		
		
		//打开对话框（选择交易对象）
		Model.triggerProviderNoClick = function(){
			
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();
     		var providerNo = dataMaster.getValue('providerNo', rowID);
     		if(providerNo!=null && providerNo!=''){
//         		dataMaster.setValue('storageTypeD', null, rowID);
//         		dataMaster.setValue('storageTypeDLabel', null, rowID);
         		dataMaster.setValue('providerID', null, rowID);
         		dataMaster.setValue('providerNo', null, rowID);
         		dataMaster.setValue('shortName', null, rowID);
         		dataMaster.setValue('providerName', null, rowID);
     			
     			return;
     		}

     		var options = {};
			options.recordState = 'none';
			options.id = dataMaster.getValue('enterpriseNo',rowID);

			var modalInstance = systemService.openProviderNoDialog(options);
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
	             try {
	             	for ( var i = 0; i < returnData.length; i++) {
//	             		alert(returnData[i].sID+'-->'+returnData[i].sFName);
//	             		dataMaster.setValue('storageTypeD', returnData[i].storageTypeD, rowID);
//	             		dataMaster.setValue('storageTypeDLabel', returnData[i].storageTypeDLabel, rowID);
	             		dataMaster.setValue('providerID', returnData[i].fID, rowID);
	             		dataMaster.setValue('providerNo', returnData[i].providerNo, rowID);
	             		dataMaster.setValue('shortName', returnData[i].shortName, rowID);
	             		dataMaster.setValue('providerName', returnData[i].providerName, rowID);
	         			
	                 }
	             } finally {
	             	return;
	             }

	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
		};


		//打开对话框（选择经办人）
		Model.triggerEmployeeNoClick = function(){
			
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();
     		var employeeNo = dataMaster.getValue('employeeNo', rowID);
     		if(employeeNo!=null && employeeNo!=''){
         		dataMaster.setValue('employeeNo', null, rowID);
         		dataMaster.setValue('employeeName', null, rowID);
     			return;
     		}
			
			
			var options = {};
			options.id = dataMaster.getValue('enterpriseNo',rowID);
			//是否显示人员
			options.psm = true;

			var modalInstance = apiService.openOrgNoDialog(options);
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
	             try {
	             	for ( var i = 0; i < returnData.length; i++) {
//	             		alert(returnData[i].sID+'-->'+returnData[i].sFName);
	             		dataMaster.setValue('employeeNo', returnData[i].sFID, rowID);
	             		dataMaster.setValue('employeeName', returnData[i].sFName, rowID);
	         			
	                 }
	             } finally {
	             	return;
	             }

	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
		}

		Model.itemSelectSingle = function(simpleRow){
     		var dataDetail = Model.dataDetail;
     		var rowID = dataDetail.getCurrentID();

     		//设置仓库
 			dataDetail.setValue('storageType', simpleRow.storageType, rowID);
 			dataDetail.setValue('storageTypeLabel', simpleRow.storageTypeLabel, rowID);
 			dataDetail.setValue('storageID', simpleRow.storageID, rowID);
 			dataDetail.setValue('storageNo', simpleRow.storageNo, rowID);
 			dataDetail.setValue('storageName', simpleRow.storageName, rowID);
 			dataDetail.setValue('floorNo', simpleRow.floorNo, rowID);
 			
 			dataDetail.setValue('itemID', simpleRow.fID, rowID);
 			dataDetail.setValue('itemNo', simpleRow.itemNo, rowID);
 			dataDetail.setValue('itemName', simpleRow.itemName, rowID);
 			dataDetail.setValue('itemProperty', simpleRow.itemProperty, rowID);
 			dataDetail.setValue('itemDescribe', simpleRow.itemDescribe, rowID);
 			dataDetail.setValue('unitCode', simpleRow.unitCode, rowID);
 			dataDetail.setValue('unitName', simpleRow.unitName, rowID);
 			dataDetail.setValue('validDays', simpleRow.validDays, rowID);
 			dataDetail.setValue('checkoutAgainDays', simpleRow.checkoutAgainDays, rowID);
 			
 			dataDetail.setValue('batchNo', simpleRow.batchNo, rowID);
		};

		//打开对话框（选择物料）
		Model.grid_itemNoButtonClick = function(){
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();
			var options = {};
			
			options.recordState = 'none';
			options.id = dataMaster.getValue('enterpriseNo',rowID);

			var modalInstance = systemService.openItemNoDialog(options);
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	
	         	var returnData = event.data;
	         	//debugger;
	             try {
	 	         	var simpleRow = event.data[0];
	 	         	Model.itemSelectSingle(simpleRow);
	             } finally {
	             	return;
	             }

	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
		}
		
		//打开多选对话框（选择物料）
		Model.addrows = function(){
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();
			var options = {};
			
			options.recordState = 'none';
			options.id = dataMaster.getValue('enterpriseNo',rowID);
			var modalInstance = systemService.openItemNosDialog(options);
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
				var returnData = event.data;
		    	for ( var i = 0; i < returnData.length; i++) {
		    		//新增行
		    		Model.addrow();
		    		var simpleRow = returnData[i];
		    		Model.itemSelectSingle(simpleRow);
		    	}
	        	
	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
		}
		
		//打开对话框（选择仓库）
		Model.grid_floorNoButtonClick = function(){

     		var storageID = Model.dataMaster.getValue('storageID', Model.dataMaster.getCurrentID());

     		var dataDetail = Model.dataDetail;
     		var rowID = dataDetail.getCurrentID();
     		
			var options = {};
			options.recordState = 'none';
			options.id = dataDetail.getValue('enterpriseNo',rowID);
			options.factoryID = dataDetail.getValue("factoryID",rowID);
			options.storageID = storageID;
			
			var modalInstance = systemService.openStorageNoDialog(options);

	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
     			dataDetail.setValue('storageType', returnData[0].storageType, rowID);
     			dataDetail.setValue('storageTypeLabel', returnData[0].storageTypeLabel, rowID);
     			dataDetail.setValue('storageID', returnData[0].fID, rowID);
     			dataDetail.setValue('storageNo', returnData[0].storageNo, rowID);
     			dataDetail.setValue('storageName', returnData[0].storageName, rowID);
     			dataDetail.setValue('floorNo', returnData[0].storageFullName, rowID);

	        }, function (reason) {  
	            console.info('用户取消了操作 at: ' + new Date());  
	        });  
			
		};
		
		
		/////////////////////////////////////////////////////////////////////
		Model.triggerConfirmClick = function(event){
			// 提交单据
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invCBKZ/single/confirm';
			systemService.operateBill(data);
		};
		Model.triggerActiveClick = function(event){
			// 撤销提交
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invCBKZ/single/active';
			systemService.operateBill(data);
		};
		
		Model.triggerAgreeItClick = function(event){
			// 审核单据
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invCBKZ/single/agreeIt';
			systemService.operateBill(data);
		};
		Model.triggerUnAgreeClick = function(event){
			// 撤销审核
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invCBKZ/single/unAgree';
			systemService.operateBill(data);
		};
		
		Model.triggerCloseClick = function(event){
			// 关闭单据
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invCBKZ/single/close';
			systemService.operateBill(data);
		};
		Model.triggerOpenClick = function(event){
			// 撤销审核
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invCBKZ/single/open';
			systemService.operateBill(data);
		};

		///////////////////////////////////////////////////////////////////////////////
		
		
		//双击事件
		Model.gridListRowDblclick = function(rowIndex,rowID){
			$scope.tabpanel = 'single';
		}
		

		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//		Model.objects = {"dataMaster" : Model.dataMaster,"dataDetail" : Model.dataDetail};
		
		var options = {};
		options.container = Model;
		options.concept = 'StorageLedger';
		options.name = 'dataMaster';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/inventory/invCBKZ/single/create';
		options.reader = '/inventory/invCBKZ/single/query';
		options.writer = '/inventory/invCBKZ/single/save';
		options.deleter = '/inventory/invCBKZ/single/delete';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
		options.master = {"data":"","relation":""};
		options.directDeleteMode = true;
		options.limit = 15;
		options.orderBy = "recordNo DESC";

		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataMasterIndexChanged;
		options.onBeforeNew = Model.dataMasterBeforeNew;
		options.onAfterNew = Model.dataMasterAfterNew;
		options.onBeforeDelete = Model.dataMasterBeforeDelete;
		options.onAfterDelete = Model.dataMasterAfterDelete;
		options.onValueChanged = Model.dataMasterValueChanged;
		options.onDataChanged = Model.dataMasterDataChanged;
		options.onBeforeSave = Model.dataMasterBeforeSave;
		options.onAfterSave = Model.dataMasterAfterSave;
		options.onNewCreateParam = Model.dataMasterNewCreateParam;
		
		Model.dataMaster = new BizData(options);
//		Model.objects.dataMaster = Model.dataMaster;
		//监视对象中的所有变动
		Model.dataMaster.deregRows = $scope.$watch('Model.dataMaster.rows',Model.dataMaster.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值

		
		
		var options = {};
		options.container = Model;
		options.concept = 'StorageLedgerDetail';
		options.name = 'dataDetail';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/inventory/invCBKZ/single/createDetail';
		options.reader = '/inventory/invCBKZ/single/queryDetail';
		options.writer = '';
		options.deleter = '';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
		options.master = {"data":"dataMaster","relation":"fMasterID"};
		options.directDeleteMode = false;
		options.orderBy = 'serialNo ASC';
		

		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataDetailIndexChanged;
		options.onBeforeNew = Model.dataDetailBeforeNew;
		options.onAfterNew = Model.dataDetailAfterNew;
		options.onBeforeDelete = Model.dataDetailBeforeDelete;
		options.onAfterDelete = Model.dataDetailAfterDelete;
		options.onValueChanged = Model.dataDetailValueChanged;
		options.onDataChanged = Model.dataDetailDataChanged;
//		options.rowsWatcherFn = Model.rowsWatcherFn;
		
		Model.dataDetail = new BizData(options);
//		Model.objects.dataDetail = Model.dataDetail;
		//监视对象中的所有变动
		Model.dataDetail.deregRows = $scope.$watch('Model.dataDetail.rows',Model.dataDetail.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		//增加过滤器，只显示本人及本公司的数据
		Model.dataMaster.filter = "(StorageLedger.billType='CBKZ' OR StorageLedger.billType='CBTZ')";
		
		Model.dataMaster.refreshData();
		
		$scope.tabpanel = 'list';
		
	    return Model;
	}	
	
